<?php
/**
 * Created by PhpStorm.
 * User: Manlin
 * Date: 2019/7/30
 * Time: 下午2:45
 */
namespace App\Storage;

use EasySwoole\Component\Singleton;
use EasySwoole\MysqliPool\Mysql;

class ChatAutoReply
{
    use Singleton;
    protected $table = 'ven_chat_autoreply';

    /**
     * sample:['keyword':'','content':'','status':0|1,'shop_id':'','created':datetime]
     * @param array $data
     */
    public function set(array $data): void
    {
        $db = Mysql::getInstance()->pool('mysql')->getObj();
        if ($db) {
            $db->insert($this->table, $data);
            Mysql::getInstance()->pool('mysql')->recycleObj($db);
        }
    }

    /**
     * @param array $where
     * @return array
     */
    public function get(array $where)
    {
        $db = Mysql::getInstance()->pool('mysql')->getObj();
        if ($db) {
            $db->where('status', 1, '=', 'and');
            if (is_array($where)) {
                foreach ($where as $key => $val) {
                    $db->where($key, $val, '=', 'and');
                }
            }
            $data = $db->get($this->table);
            Mysql::getInstance()->pool('mysql')->recycleObj($db);
            if ($data) {
                return $data;
            }
        }
        return [];
    }

    /**
     * @param array $where
     * @param array $data
     * @return bool
     */
    public function update(array $where, array $data): bool {
        $db = Mysql::getInstance()->pool('mysql')->getObj();
        if ($db) {
            $res = $db->where($where)->update($this->table, $data);
            Mysql::getInstance()->pool('mysql')->recycleObj($db);
            return $res;
        }
    }
}